Regular Expression help

I want a single regular expression that will be true for strings starting with "BPv2" but not for strings starting with "BPv2.5"
Sounds simple, right?  If I read the DXL help right, this should work:

Regexp rxBP = regexp "^BPv2[^\\.]"

But noooooo . . .   "BPv2" gets a false.  I've tried several variations, (some also using regexp2), but have not yet found the single magic RX.  NOTE:  Due to the existing code this will go into, I am constrained to use just one regexp, as opposed to 2 separate regexp's AND-ed together, such as:

if (matches("^BPv2", myString) and !matches("^BPv2.5", myString))

if there is no single regexp that satisfies, I may have to re-write the code that this is a part of, to use the multi-regexp method.  Any help is appreciated!

 


Thosicus - Tue Jun 11 17:38:39 EDT 2013

Re: Regular Expression help
Richard_Hesketh - Wed Jun 12 02:54:32 EDT 2013

Hi Thosicus

I think some clarification of your requirement is needed. Do you need to identify text that contains only 'BPv2', or do you also need 'BPv2.1'-'BPv2.4', but excluding 2.5? What about values higher than 2.5?

If what you want is only values of 'BPv2', with absolutely nothing else before or after in the string, how about:  ^BPv2$

Your expression fails because the part in square brackets is required - in other words, you're requiring at least one character after 'BPv2', but asserting only that it must not be a '.'

You may be aware that you can add a '*' after the square brackets to denote that the character(s) may occur zero or more times, or a '+' to denote that it occurs at least once, but neither will help in this specific case.

I hope this helps - if you're able to clarify your requirement, perhaps more can be done.

Regards,

Richard

Re: Regular Expression help
Thosicus - Wed Jun 12 11:18:44 EDT 2013

Richard_Hesketh - Wed Jun 12 02:54:32 EDT 2013

Hi Thosicus

I think some clarification of your requirement is needed. Do you need to identify text that contains only 'BPv2', or do you also need 'BPv2.1'-'BPv2.4', but excluding 2.5? What about values higher than 2.5?

If what you want is only values of 'BPv2', with absolutely nothing else before or after in the string, how about:  ^BPv2$

Your expression fails because the part in square brackets is required - in other words, you're requiring at least one character after 'BPv2', but asserting only that it must not be a '.'

You may be aware that you can add a '*' after the square brackets to denote that the character(s) may occur zero or more times, or a '+' to denote that it occurs at least once, but neither will help in this specific case.

I hope this helps - if you're able to clarify your requirement, perhaps more can be done.

Regards,

Richard

Hi Richard.  I had tried the * and the + characters to no avail in this case, as you said.   Here are some example strings with desired rx results:

BPv2               ->  true
BPv2BPv3     ->  true
BPv2.5           ->  false
BPv2.5BPv3  ->  false
BPv2.5BPv3  ->  false

The original attribute values are linebreak-separated, as in ...

"BPv2.5
BPv3"

...but I added a little function in the code to remove the line breaks, as I thought it would make the rx checking work.  It didn't.

Thanks,

T

Re: Regular Expression help
llandale - Wed Jun 12 13:07:09 EDT 2013

Maybe there is a couple more test cases, but this seems to work:

  • ("^(BPv2\\.*|BPv2[^\\.].*|BPv2.[^5].*)$")  

I think it says: Any string that matches any of these conditions:

  • BPv2\\.*:  Exactly BPv2 with zero or more periods and nothing else
  • BPv2[^\\.].*  Exactly BPv2 followed by any non-period character, and zero or more any characters
  • BPv2.[^5].*  Exactly BPv2. followed by any non-5 character, and zero or more any characters.

Here's the code I used to test it.

string sBP
Regexp rxBP
void Set(string sRe)
{ sBP = sRe
 rxBP = regexp(sBP)
}

void Test(string s)
{ print (rxBP s) "\t[" s "]\n"
}

// Set("^(BPv2)$")    // true false++
//Set("^(BPv2|BPv2[^\\.].*)$")   // true true false++
// Set("^(BPv2|BPv2[^\\.].*|BPv2.[^5].*)$")  // true true false true false
Set("^(BPv2\\.*|BPv2[^\\.].*|BPv2.[^5].*)$")  // true true true true false

 print "\t\tsBP = {" sBP "}\n"
Test("BPv2")
Test("BPv2BPv3")
Test("BPv2.")
Test("BPv2.BPv3")
Test("xBPv2")
Test("BPv2.5")
Test("BPv2.5BPv3")
Test("BPv2.5BPv2")     // note "BPv2" not in first position

-Louie

Re: Regular Expression help
Thosicus - Wed Jun 12 13:56:33 EDT 2013

llandale - Wed Jun 12 13:07:09 EDT 2013

Maybe there is a couple more test cases, but this seems to work:

  • ("^(BPv2\\.*|BPv2[^\\.].*|BPv2.[^5].*)$")  

I think it says: Any string that matches any of these conditions:

  • BPv2\\.*:  Exactly BPv2 with zero or more periods and nothing else
  • BPv2[^\\.].*  Exactly BPv2 followed by any non-period character, and zero or more any characters
  • BPv2.[^5].*  Exactly BPv2. followed by any non-5 character, and zero or more any characters.

Here's the code I used to test it.

string sBP
Regexp rxBP
void Set(string sRe)
{ sBP = sRe
 rxBP = regexp(sBP)
}

void Test(string s)
{ print (rxBP s) "\t[" s "]\n"
}

// Set("^(BPv2)$")    // true false++
//Set("^(BPv2|BPv2[^\\.].*)$")   // true true false++
// Set("^(BPv2|BPv2[^\\.].*|BPv2.[^5].*)$")  // true true false true false
Set("^(BPv2\\.*|BPv2[^\\.].*|BPv2.[^5].*)$")  // true true true true false

 print "\t\tsBP = {" sBP "}\n"
Test("BPv2")
Test("BPv2BPv3")
Test("BPv2.")
Test("BPv2.BPv3")
Test("xBPv2")
Test("BPv2.5")
Test("BPv2.5BPv3")
Test("BPv2.5BPv2")     // note "BPv2" not in first position

-Louie

Hi Louie,

Amazing!   That worked -- for my initial set of test cases.   When I tried a BPv2.7 (which I was told was a possible value), it gave a true;  so I tried removing the last rx chunk that deals with BPv2.5, and it then gave a correct false for BPv2.5 as well as for BPv2.7.  It also gave correct true/false results for my other test strings.  The resulting regexp in my test code now looks like this:

Regexp BPOK = regexp ("^(BPv2\\.*|BPv2[^\\.].*)$")    // Louie Landale's suggestion, edited
 

Kudos and high-fives to you Mr. Landale.

-T

Re: Regular Expression help
llandale - Wed Jun 12 14:26:48 EDT 2013

Thosicus - Wed Jun 12 13:56:33 EDT 2013

Hi Louie,

Amazing!   That worked -- for my initial set of test cases.   When I tried a BPv2.7 (which I was told was a possible value), it gave a true;  so I tried removing the last rx chunk that deals with BPv2.5, and it then gave a correct false for BPv2.5 as well as for BPv2.7.  It also gave correct true/false results for my other test strings.  The resulting regexp in my test code now looks like this:

Regexp BPOK = regexp ("^(BPv2\\.*|BPv2[^\\.].*)$")    // Louie Landale's suggestion, edited
 

Kudos and high-fives to you Mr. Landale.

-T

"Mr. Landale" died 15 years ago.  Thanks for reminding me.   lol

Your expression fails for "BPv2.BPv3" when it should pass.

I think your new requirements are for it to pass for major version "2" but to fail for any well-formed minor version, 2.1, 2.2, ... 2.9.  I'm guessing that "2.0" should pass.  If so, replace the "5" in my string with "1-9"; which then looks like:

  • "^(BPv2\\.*|BPv2[^\\.].*|BPv2.[^1-9].*)$"

ButWAIT!  Should the following string pass or fail?

  • BPv2.01

But WAIT!  The "^" and "$" characters, when at the start and end of the expression, means start or end of the line, even though the manual says start/end of the string.  Since we know that a two "lines" have the '\n' end-of-line delimiter, I see that my expression PASSES the following input string when it should FAIL:

  • "\nBPv2"

Wikipedia Regular Expressions talks about "greedy" and "non-greedy" expressions and I'm pretty sure the solution can be found by playing with that.

Regular Expressions always give me Regular Headaches.

-Louie

Re: Regular Expression help
Thosicus - Wed Jun 12 16:08:38 EDT 2013

llandale - Wed Jun 12 14:26:48 EDT 2013

"Mr. Landale" died 15 years ago.  Thanks for reminding me.   lol

Your expression fails for "BPv2.BPv3" when it should pass.

I think your new requirements are for it to pass for major version "2" but to fail for any well-formed minor version, 2.1, 2.2, ... 2.9.  I'm guessing that "2.0" should pass.  If so, replace the "5" in my string with "1-9"; which then looks like:

  • "^(BPv2\\.*|BPv2[^\\.].*|BPv2.[^1-9].*)$"

ButWAIT!  Should the following string pass or fail?

  • BPv2.01

But WAIT!  The "^" and "$" characters, when at the start and end of the expression, means start or end of the line, even though the manual says start/end of the string.  Since we know that a two "lines" have the '\n' end-of-line delimiter, I see that my expression PASSES the following input string when it should FAIL:

  • "\nBPv2"

Wikipedia Regular Expressions talks about "greedy" and "non-greedy" expressions and I'm pretty sure the solution can be found by playing with that.

Regular Expressions always give me Regular Headaches.

-Louie

Master Landale,

The attribute this is looking at is a multi-enum type consisting of these strings:

787
BPv1
BPv1A
BPv2
BPv2.5
BPv3

Anything starting with BPv2 (but not BPv2.5) is to be counted.   So I think we're covered now, THANKS to you.

You think you've got it bad?  Regular expressions make me irregular.

-T

Re: Regular Expression help
Adamarla - Thu Jun 13 01:54:32 EDT 2013

Thosicus - Wed Jun 12 16:08:38 EDT 2013

Master Landale,

The attribute this is looking at is a multi-enum type consisting of these strings:

787
BPv1
BPv1A
BPv2
BPv2.5
BPv3

Anything starting with BPv2 (but not BPv2.5) is to be counted.   So I think we're covered now, THANKS to you.

You think you've got it bad?  Regular expressions make me irregular.

-T

 

If you need to future proof this against adding a "BPv20, BPv21, BPv23..." enumeration I would use this:

Regexp rxBP = regexp2("^BPv2([^\\.0-9]|$)")
 
void Test(string s)
{
print (rxBP s) "\t[" s "]\n"
}
 
Test("BPv2")
Test("BPv22")
Test("BPv222")
Test("BPv2.1")
Test("BPv2.2")
Test("BPv1BPv2")  // Remove '^' prefix to match this too
Test("BPv1BPv22")
Test("BPv2BPv3")
Test("BPv22BPv3")

delete(rxBP)

-Adam

 

Re: Regular Expression help
llandale - Fri Jun 14 15:43:22 EDT 2013

Thosicus - Wed Jun 12 16:08:38 EDT 2013

Master Landale,

The attribute this is looking at is a multi-enum type consisting of these strings:

787
BPv1
BPv1A
BPv2
BPv2.5
BPv3

Anything starting with BPv2 (but not BPv2.5) is to be counted.   So I think we're covered now, THANKS to you.

You think you've got it bad?  Regular expressions make me irregular.

-T

Then your requirements are "if it is exactly 'BPv2'"; forget about "starts with".